package com.cmfi.controller.PlcController;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cmfi.domain.PlcEntity.PlcNode;
import com.cmfi.service.inter.PlcInter.PlcNodeService;
import com.cmfi.utils.Code;
import com.cmfi.utils.Result;

@RestController
@RequestMapping("/api")
public class PlcNodeController {

	private final Logger log = LoggerFactory.getLogger(PlcNodeController.class);
	@Autowired
	private PlcNodeService plcNodeService;

	@PostMapping("/plcNode")
	public Result addPlcNode(@RequestBody PlcNode plc) {
		try {
			plcNodeService.addPlcNode(plc);
			return new Result(Code.SAVE_OK, "");
		} catch (Exception e) {
			log.error("[PlcController][addPlcNode]  e,", e);
			return new Result(Code.SAVE_ERR, e.getMessage());
		}
	}

	@DeleteMapping("/plcNode/{id}")
	public Result delPlcNode(@PathVariable(value = "id", required = false) final Long id) {
		try {
			plcNodeService.delPlcNode(id);
			return new Result(Code.DELETE_OK, id);
		} catch (Exception e) {
			log.error("[PlcController][delPlcNode]  e,", e);
			return new Result(Code.DELETE_OK, e.getMessage());
		}
	}

	/**
	 * {@code GET  /forklift-qualities} : get all the forkliftQualities.
	 *
	 * @param pageable the pagination information.
	 * @param request  a {@link ServerHttpRequest} request.
	 * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list
	 *         of forkliftQualities in body.
	 */
	@GetMapping("/plcNodes")
	public Result getAllPlcNodes() {
		try {
			List<PlcNode> plcList = plcNodeService.getAllPlcNode();
			return new Result(Code.GET_OK, plcList);
		} catch (Exception e) {
			log.error("[PlcController][getAllPlcNodes] e,", e);
			return new Result(Code.GET_ERR, e.getMessage());
		}
	}

}
